package com.hyj.ecp.clientservice.cust.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ecp.cust.ClientUserQO;
import com.ecp.cust.CustAddressee;
import com.ecp.cust.CustProblem;
import com.hyj.ecp.clientservice.cust.service.IClientLoginService;
import com.hyj.util.GzipUtil;
import com.hyj.util.HttpUtil;
import com.hyj.util.JsonUtil;

@Controller
@RequestMapping("/client/cust/*")
public class ClientLoginAction {

	
	Log logger = LogFactory.getLog(ClientLoginAction.class);
	@Autowired
    private IClientLoginService service;
	
	
	/**
	 * 默认 是request set  map 的所有属性值
	 * @param u
	 * @param map
	 * @return
	 * @throws IOException 
	 */
    @RequestMapping(value="login.do", method=RequestMethod.POST)
    public void login(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
		logger.info("login.do start...");
		//String userNum=request.getHeader("userNum");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			Map<?,?> m = (Map<?,?>) JsonUtil.fromJson(reqparam, Map.class);
			String account  = (String) m.get("Account");
			String password  = (String) m.get("Password");
			
			ClientUserQO qo = new ClientUserQO();
		    qo.setAccount(account);
		    qo.setPassword(password);
			
		    ClientUserQO retCode = service.login(qo);
			if (retCode != null) {
				map.put("ReturnCode", 0);
				map.put("msg", "登入成功");
				map.put("User", retCode);
			} else {
				map.put("ReturnCode", 999);
				map.put("msg", "用户名或者密码错误");
			}
		}catch(Exception e){
			logger.error("login.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = null;
		String encoding = request.getHeader("Accept-Encoding");
		if (encoding != null && encoding.indexOf("gzip") != -1) {
			response.setHeader("Content-Encoding", "gzip");
			out = GzipUtil.getGzipOutputStream(response.getOutputStream());
		} else {
			out = response.getOutputStream();
		}
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("login.do end");
    	
    }
    
    
    /**
	 * 注册接口
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "register.do", method = RequestMethod.POST)
	public void register(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		//response.setHeader("Accept", MobileConstant.Header_Accept);
		//response.setContentType(MobileConstant.ContentType);
		logger.info("register.do start...");
		//String userNum=request.getHeader("userNum");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			Map<?,?> m = (Map<?,?>) JsonUtil.fromJson(reqparam, Map.class);
			String account  = (String) m.get("Account");
			String password  = (String) m.get("Password");
			String name  = (String) m.get("name");
			String cardno  = (String) m.get("cardno");
			
			ClientUserQO qo = new ClientUserQO();
		    qo.setAccount(account);
		    qo.setPassword(password);
			
		    int retCode = service.saveUser(qo);
		    if(retCode==0){
		    map.put("ReturnCode", retCode);
		    map.put("msg", "注册成功");
		    }else if(retCode==1){
		    	 map.put("ReturnCode", retCode);
				 map.put("msg", "账户已经注册，请换个账户");
		    }else{
		    	 map.put("ReturnCode", retCode);
				 map.put("msg", "注册失败");
		    }
		}catch(Exception e){
			logger.error("register.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = null;
		String encoding = request.getHeader("Accept-Encoding");
		if (encoding != null && encoding.indexOf("gzip") != -1) {
			response.setHeader("Content-Encoding", "gzip");
			out = GzipUtil.getGzipOutputStream(response.getOutputStream());
		} else {
			out = response.getOutputStream();
		}
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("register.do end");
		 
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "updateUser.do", method = RequestMethod.POST)
	public void updateUser(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		//response.setHeader("Accept", MobileConstant.Header_Accept);
		//response.setContentType(MobileConstant.ContentType);
		logger.info("updateUser.do start...");
		//String userNum=request.getHeader("userNum");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			ClientUserQO m = JsonUtil.fromJson(reqparam, ClientUserQO.class);
            
			
		    int retCode = service.updateUser(m);
		    if(retCode==0){
		    map.put("ReturnCode", retCode);
		    map.put("msg", "修改成功");
		    }else if(retCode==1){
		    	 map.put("ReturnCode", retCode);
				 map.put("msg", "密码错误");
		    }else{
		    	 map.put("ReturnCode", retCode);
				 map.put("msg", "修改失败");
		    }
		}catch(Exception e){
			logger.error("register.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = null;
		String encoding = request.getHeader("Accept-Encoding");
		if (encoding != null && encoding.indexOf("gzip") != -1) {
			response.setHeader("Content-Encoding", "gzip");
			out = GzipUtil.getGzipOutputStream(response.getOutputStream());
		} else {
			out = response.getOutputStream();
		}
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("updateUser.do end");
		 
	}
	
	
	/**
	 * 个人地址查询
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "queryCustAddr.do", method = RequestMethod.POST)
	public void queryCustAddr(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		logger.info("queryCustAddr.do start...");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			CustAddressee m = JsonUtil.fromJson(reqparam, CustAddressee.class);
            
		    List<Map<String,Object>> list = service.queryCustAddr(m);
		    map.put("ReturnCode", 0);
		    map.put("List", list);
		}catch(Exception e){
			logger.error("queryCustAddr.do 出错",e);
			map.put("ReturnCode", 999);
			map.put("msg", e.getMessage());
			map.put("List", new Object[]{});
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("queryCustAddr.do end");
		 
	}
	
	@RequestMapping(value = "insertCustAddr.do", method = RequestMethod.POST)
	public void insertCustAddr(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		logger.info("insertCustAddr.do start...");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			CustAddressee m = JsonUtil.fromJson(reqparam, CustAddressee.class);
            
		    int retCode = service.saveCustAddr(m);
		    if(retCode==0){
			    map.put("ReturnCode", retCode);
			    map.put("msg", "保存成功");
		    }else{
		    	map.put("ReturnCode", retCode);
				map.put("msg", "保存失败");
		    }
		}catch(Exception e){
			logger.error("insertCustAddr.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
			map.put("msg", e.getMessage());
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("insertCustAddr.do end");
		 
	}
	
	@RequestMapping(value = "updateCustAddr.do", method = RequestMethod.POST)
	public void updateCustAddr(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		logger.info("updateCustAddr.do start...");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			CustAddressee m = JsonUtil.fromJson(reqparam, CustAddressee.class);
            
		    int retCode = service.updateCustAddr(m);
		    if(retCode==0){
			    map.put("ReturnCode", retCode);
			    map.put("msg", "修改成功");
		    }else{
		    	map.put("ReturnCode", retCode);
				map.put("msg", "修改失败");
		    }
		}catch(Exception e){
			logger.error("updateCustAddr.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
			map.put("msg", e.getMessage());
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("updateCustAddr.do end");
		 
	}
	
	/**
	 * 删除 用户收件地址
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "deleteCustAddr.do", method = RequestMethod.POST)
	public void deleteCustAddr(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		logger.info("deleteCustAddr.do start...");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			CustAddressee m = JsonUtil.fromJson(reqparam, CustAddressee.class);
            
		    int retCode = service.deleteCustAddr(m);
		    if(retCode==0){
			    map.put("ReturnCode", retCode);
			    map.put("msg", "删除成功");
		    }else{
		    	map.put("ReturnCode", retCode);
				map.put("msg", "删除失败");
		    }
		}catch(Exception e){
			logger.error("deleteCustAddr.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
			map.put("msg", e.getMessage());
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("deleteCustAddr.do end");
		 
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	@RequestMapping(value = "saveCustProblem.do", method = RequestMethod.POST)
	public void saveCustProblem(HttpServletRequest request,HttpServletResponse response 
			) throws UnsupportedEncodingException, IOException {
		logger.info("saveCustProblem.do start...");
		BufferedReader read = request.getReader();
		String ss = null;
		StringBuffer sbf  = new StringBuffer();
		while ((ss = read.readLine()) != null) {
			 sbf.append(ss);
		}
		String reqparam = sbf.toString();
		logger.info("请求参数" + reqparam);
		Map<String, Object>  map  = new HashMap<String,Object>();
		try{
			CustProblem m = JsonUtil.fromJson(reqparam, CustProblem.class);
            
		    int retCode = service.saveCustProblem(m);
		    if(retCode==0){
			    map.put("ReturnCode", retCode);
			    map.put("msg", "修改成功");
		    }else{
		    	map.put("ReturnCode", retCode);
				map.put("msg", "修改失败");
		    }
		}catch(Exception e){
			logger.error("saveCustProblem.do 出错",e);
			map = new HashMap<String,Object>();
			map.put("ReturnCode", 999);
			map.put("msg", e.getMessage());
		}
		 
		String json = JsonUtil.toJson(map, false);
		
		logger.info("返回参数：" + json);
		OutputStream out = HttpUtil.writeResponse(request, response);
		out.write(json.getBytes("UTF-8"));
		out.flush();
		out.close();
		logger.info("saveCustProblem.do end");
		 
	}
	
}
